package com.example.yt_app.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;


import com.example.yt_app.R;

import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 *
 * @author dell
 * @date 2021/7/21
 */

@SuppressWarnings("ALL")
public class SbAdapter extends BaseAdapter {
    List<Map<String, Object>>list;
    LayoutInflater inflater;


    public SbAdapter(Context context,List<Map<String, Object>>list){
        this.list = list;
        inflater = LayoutInflater.from(context);
    }

    /**
     * 返回item的个数
     * @return
     */
    @Override
    public int getCount() {
        return list.size();
    }

    /**
     * 返回每一个item对象
     * @param i
     * @return
     */
    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    /**
     * 返回每一个item的id
     * @param i
     * @return
     */
    @Override
    public long getItemId(int i) {
        return i;
    }




    /**
     * @param index
     * @param listview
     *
     * @return
     */
    public void update(int index,ListView listview){
        ViewHolder vh;
        vh = new ViewHolder();
        //得到第一个可见item项的位置
        int visiblePosition = listview.getFirstVisiblePosition();
        //得到指定位置的视图，对listview的缓存机制不清楚的可以去了解下
        View view = listview.getChildAt(index - visiblePosition);
        ViewHolder holder = (ViewHolder) view.getTag();

        Map<String, Object>map = list.get(index);
        setData(holder,index);

    }
    private void setData(ViewHolder holder,int index){
        ViewHolder vh;
        vh = new ViewHolder();
        Map<String, Object>map = list.get(index);
        vh.online.setText(map.get("online").toString());
    }
    /**
     * @param i
     * @param view
     * @param viewGroup
     * @return
     */
    @SuppressWarnings({"AlibabaUndefineMagicConstant", "AlibabaRemoveCommentedCode"})
    @SuppressLint("ResourceAsColor")
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder vh;
        vh = new ViewHolder();
        if (view == null){
            view = inflater.inflate(R.layout.simple_adapter_item, null);
            vh.id = (TextView)view.findViewById(R.id.sai_id);
            vh.name = (TextView)view.findViewById(R.id.sai_name);
            vh.online = (TextView)view.findViewById(R.id.sai_phone);


            view.setTag(vh);
        } else {
            vh = (ViewHolder)view.getTag();
        }
        vh.id.setText(list.get(i).get("id").toString());
        vh.name.setText(list.get(i).get("name").toString());

        vh.online.setText(list.get(i).get("online").toString());
        //noinspection AlibabaUndefineMagicConstant,AlibabaUndefineMagicConstant
        if (list.get(i).get("online").toString() == "true"){
            //String o = String.valueOf(devices.get(i).getOnline());
            vh.online.setTextColor(Color.GREEN);
        }else {
            vh.online.setTextColor(Color.RED);
        }

        //...
        //此处需要返回view 不能是view中某一个
        return view;
    }
    public final class ViewHolder
    {
        public TextView id;
        public TextView name;
        public TextView online;
    }

}
